#!/bin/bash
#
# Copyright 2009-2011 The VOTCA Development Team (http://www.votca.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

version='@PROJECT_VERSION@ #CSG_HG_ID#'
ext_cmd=""
simprog=""
log=""
show_share="no"

#unset stuff from enviorment
unset CSGXMLFILE CSGDEBUG bondtype bondname

#failback define
die() {
  echo -e "$*" >&2
  exit 1
}

show_help () {
  cat << eof
==================================================
========   VOTCA (http://www.votca.org)   ========
==================================================

please submit bugs to bugs@votca.org

${0##*/}, version ${version}

This script calls scripts and functions for the iterative framework.
Function can be executed or shows if key1='function'.

Usage: ${0##*/} [OPTIONS] key1 key2 [SCRIPT OPTIONS]

Allowed options:
-l, --list           Show list of all script
    --cat            Show the content of the script
    --show           Show the path to the script
    --show-share     Shows the used VOTCASHARE dir and exits
    --scriptdir DIR  Set the user script dir
                     (Used if no options xml file is given)
                     Default: empty
    --options FILE   Specify the options xml file to use
    --log FILE       Specify the log file to use
                     Default: stdout
    --ia-type type   Specify the interaction type to use
    --ia-name name   Specify the interaction name to use
    --nocolor        Disable colors
    --sloppy-tables  Allow tables without flags
    --debug          Enable debug mode with a lot of information
-h, --help           Show this help

Examples:
* ${0##*/} table smooth [ARGUMENTS]
* ${0##*/} --show run gromacs
eof
}


while [[ ${1#-} != $1 ]]; do
 if [[ ${1#--} = $1 && -n ${1:2} ]]; then
    #short opt with arguments here: fc
    if [[ ${1#-[fc]} != ${1} ]]; then
       set -- "${1:0:2}" "${1:2}" "${@:2}"
    else
       set -- "${1:0:2}" "-${1:2}" "${@:2}"
    fi
 fi
 case $1 in
   -l | --list)
    ext_cmd="show_csg_tables"
    shift ;;
   --scriptdir)
    die "'--scriptdir' is obsolete, please specify the script path in the xml file (cg.inverse.scriptpath)"
    shift 2;;
   --simprog)
    die "'--simprog' is obsolete, please specify the simprog in the xml file (cg.inverse.program)"
    shift 2;;
   --options)
    export CSGXMLFILE="$2"
    [ -f "$CSGXMLFILE" ] || die "options xml file '$CSGXMLFILE' not found"
    shift 2;;
   --sloppy-tables)
    export VOTCA_TABLES_WITHOUT_FLAG="yes"
    shift ;;
   --log)
    log="$2"
    shift 2;;
   --ia-type)
    export bondtype="$2"
    shift 2;;
   --ia-name)
    export bondname="$2"
    shift 2;;
   --cat)
    ext_cmd="cat_external"
    shift;;
   --show)
    ext_cmd="source_wrapper"
    shift;;
   --show-share)
    show_share="yes"
    shift;;
   --nocolor)
    export CSGNOCOLOR="yes"
    shift;;
   -h | --help)
    show_help
    exit 0;;
   --debug)
    export CSGDEBUG="yes"
    shift;; 
   -v | --version)
    echo "${0##*/}, version $version"
    exit 0;;
  *)
   die "Unknown option '$1'";;
 esac
done

if [[ -z ${VOTCASHARE} ]]; then
  if [ -f "${0%/*}/../share/votca/scripts/inverse/inverse.sh" ]; then
    #transform it to a global path
    export VOTCASHARE="$(cd ${0%/*}/../share/votca;pwd)"
  elif [ -f "@CMAKE_INSTALL_PREFIX@/@DATA@/scripts/inverse/inverse.sh" ]; then
    export VOTCASHARE="@CMAKE_INSTALL_PREFIX@/@DATA@"
  else
    echo "Error: Environment value VOTCASHARE is not defined and could not be guessed" >&2
    echo "Export VOTCASHARE or source VOTCARC.bash or VOTCARC.csh" >&2
    exit 1
  fi
else
  if [[ ! -f ${VOTCASHARE}/scripts/inverse/inverse.sh ]]; then
    echo "Error: Environment value VOTCASHARE seems to be wrong" >&2
    echo "Could not find \${VOTCASHARE}/scripts/inverse/inverse.sh" >&2
    echo "Export VOTCASHARE or source VOTCARC.bash or VOTCARC.csh" >&2
    exit 1
  fi
fi

if [ "$show_share" = "yes" ]; then
  echo "${VOTCASHARE}"
  exit 0
fi

if [[ -f ${VOTCASHARE}/scripts/inverse/start_framework.sh ]]; then
  source ${VOTCASHARE}/scripts/inverse/start_framework.sh || die "Could not source start_framework.sh"
else
  die "Could not find start_framework.sh"
fi

[[ -n ${CSGXMLFILE} ]] && scriptpath="$(csg_get_property --allow-empty cg.inverse.scriptpath)" > /dev/null
[[ -n ${scriptpath} ]] && echo "Adding '$scriptpath to csgshare" && add_to_csgshare "$scriptpath"

[[ -n ${CSGXMLFILE} ]] && simprog="$(csg_get_property --allow-empty cg.inverse.program "$simprog")"
[[ -n ${simprog} ]] && echo "We are using Sim Program: $simprog" && source_function $simprog

if [[ $ext_cmd = show_csg_tables ]]; then
  $ext_cmd
  exit $?
fi
[[ -z $1 || -z $2 ]] && die "${0##*/}: Missing argument"

if [[ -n $ext_cmd ]]; then
  $ext_cmd $1 $2
  exit $?
fi

#help of scripts should always work and be quiet
if [[ $3 = --help ]]; then
cat <<EOF
==================================================
========   VOTCA (http://www.votca.org)   ========
==================================================

please submit bugs to bugs@votca.org

EOF
  scriptname=$(source_wrapper $1 $2)
  do_external -q $1 $2 --help | sed -e "s/%version%/${version}/" -e "/^Usage/s@${scriptname##*/}@${0##*/} [OPTIONS] $1 $2@g"
  exit $?
fi

[[ -n ${log} ]] && enable_logging "$log"
[[ -n $CSGDEBUG ]] && set -x
do_external $@
exit $?
